package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import neural_network.CancerTypes;

public class ResultPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JPanel textPanel;
	JButton back;

	public ResultPanel() {
		JPanel panel2 = new JPanel();
		panel2.setLayout(new BoxLayout(panel2, BoxLayout.PAGE_AXIS));

		textPanel = new JPanel();

		String result = fillPanel();
		BorderedLabel label = new BorderedLabel(
				"Uzorak je klasifikovan kao karcinom " + result, new Color(
						0x55acee), 16);
		JPanel randompanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

		randompanel.add(textPanel);

		BoxLayout bl;
		textPanel.setLayout(bl = new BoxLayout(textPanel, BoxLayout.Y_AXIS));

		JPanel con = new JPanel(new FlowLayout(FlowLayout.LEFT));
		back = new JButton("Nazad");
		back.addActionListener(new BackAction());
		back.setAlignmentX(LEFT_ALIGNMENT);
		this.add(label);

		panel2.add(randompanel);
		this.add(panel2);
		this.add(back);

		setVisible(false);
	}

	private String fillPanel() {
		Scanner sc;
		String result = "";
		try {
			sc = new Scanner(new File("result_classification.txt"));
			int type = (int) sc.nextFloat();
			result = CancerTypes.typeToString(type);

			StringBuilder builder = new StringBuilder();
			builder.append("<html>");
			sc = new Scanner(new File("result_validation.txt"), "UTF-8");
			boolean parni = false;
			while (sc.hasNext()) {
				String line = sc.nextLine();
				int i = 0;
				builder.append("<table cellspacing=\"1px\" cellpadding=1><th colspan=\"2\" align=\"center\" bgcolor=\"#55acee\">" + line
						+ "</th>");
				while (i < 6) {
					line = sc.nextLine();
					String line2 = sc.nextLine();
					String color = parni ? "#FFFFFF" : "#C0C0C0";
					parni = !parni;
					builder.append("<tr bgcolor=\"" + color +"\"><td>" + line + "</td><td>" + line2 + "</td></tr>");
					i++;
				}
				builder.append("</table><br/>");
								
//				builder = new StringBuilder();
			}		
			builder.append("</html>");
			BorderedLabel label = new BorderedLabel(builder.toString(),
					Color.BLACK, 11, SwingConstants.CENTER, 450, 500);
			textPanel.add(label);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return result;

	}

	public class BackAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			Frame.getFrame().setReadyStatePanelVisible(true);
			Frame.getFrame().setResultPanelVisible(false);

		}

	}

}
